| Conditions | 1 |
| Paths | 1 |
| Total Lines | 205 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* global describe, it */ |
||
| 37 | describe('FullSignalK', function () { |
||
| 38 | describe('validations', function () { |
||
| 39 | it('.validSignalK passes valid signalK', function () { |
||
| 40 | var validSk = { |
||
| 41 | 'vessels': { |
||
| 42 | 'urn:mrn:imo:mmsi:230099999': { |
||
| 43 | 'mmsi': '230099999'}}, |
||
| 44 | 'version': '1.0.0', |
||
| 45 | 'self': 'vessels.urn:mrn:imo:mmsi:230099999'} |
||
| 46 | |||
| 47 | validSk.should.be.validSignalK() |
||
| 48 | }) |
||
| 49 | it('.validSignalK fails invalid signalK', function () { |
||
| 50 | var invalidSk = { |
||
| 51 | 'vessels': { |
||
| 52 | 'urn:mrn:imo:mmXX:230099999': { |
||
| 53 | 'mmsi': '230099999'}}, |
||
| 54 | 'version': '1.0.0', |
||
| 55 | 'self': 'vessels.urn:mrn:imo:mmsi:230099999'} |
||
| 56 | |||
| 57 | invalidSk.should.not.be.validSignalK() |
||
| 58 | }) |
||
| 59 | it('validSignalKDelta passes valid delta', function () { |
||
| 60 | var validDelta = { |
||
| 61 | 'context': 'bar', |
||
| 62 | 'updates': [{ |
||
| 63 | 'source': { |
||
| 64 | 'label': '', |
||
| 65 | 'type': 'NMEA0183', |
||
| 66 | 'sentence': 'GLL', |
||
| 67 | 'talker': 'II' |
||
| 68 | }, |
||
| 69 | 'timestamp': '2013-10-08T15:47:28.263Z', |
||
| 70 | 'values': [{'path': 'a.b.c', 'value': 1234}] |
||
| 71 | }] |
||
| 72 | } |
||
| 73 | validDelta.should.be.validSignalKDelta() |
||
| 74 | }) |
||
| 75 | }) |
||
| 76 | // TODO add full set of validation tests in here |
||
| 77 | |||
| 78 | it('deltaToFull with object value should produce valid SignalK', function () { |
||
| 79 | var full = skApi.deltaToFull(deltaA) |
||
| 80 | full.vessels['urn:mrn:imo:mmsi:230099999'].navigation.position.value.should.have.property('longitude') |
||
| 81 | full.vessels['urn:mrn:imo:mmsi:230099999'].navigation.position.should.have.property('$source') |
||
| 82 | full.should.be.validSignalK() |
||
| 83 | }) |
||
| 84 | |||
| 85 | it('addDelta with object value should produce full tree leaf without the .value', function () { |
||
| 86 | var fullSignalK = new FullSignalK() |
||
| 87 | fullSignalK.addDelta(deltaA) |
||
| 88 | fullSignalK.retrieve().vessels['urn:mrn:imo:mmsi:230099999'].navigation.position.value.should.have.property('longitude') |
||
| 89 | fullSignalK.retrieve().vessels['urn:mrn:imo:mmsi:230099999'].navigation.position.should.have.property('$source') |
||
| 90 | }) |
||
| 91 | |||
| 92 | it('Two deltas from different sources results in values structure', function () { |
||
| 93 | var delta = { |
||
| 94 | 'updates': [{ |
||
| 95 | 'source': { |
||
| 96 | 'label': 'n2kFromFile', |
||
| 97 | 'type': 'NMEA2000', |
||
| 98 | 'pgn': 129038, |
||
| 99 | 'src': '43' |
||
| 100 | }, |
||
| 101 | 'timestamp': '2014-08-15T19:03:21.532Z', |
||
| 102 | 'values': [{ |
||
| 103 | 'path': 'navigation.speedOverGround', |
||
| 104 | 'value': 7.09 |
||
| 105 | }] |
||
| 106 | }], |
||
| 107 | 'context': 'vessels.foo' |
||
| 108 | } |
||
| 109 | var fullSignalK = new FullSignalK() |
||
| 110 | fullSignalK.addDelta(delta) |
||
| 111 | delta.updates[0].source.src = 48 |
||
| 112 | delta.updates[0].values[0].value = 8 |
||
| 113 | fullSignalK.addDelta(delta) |
||
| 114 | fullSignalK.retrieve().vessels.foo.navigation.speedOverGround.should.have.property('value', 8) |
||
| 115 | fullSignalK.retrieve().vessels.foo.navigation.speedOverGround.should.have.property('$source') |
||
| 116 | fullSignalK.retrieve().vessels.foo.navigation.speedOverGround.values['n2kFromFile.43'].should.have.property('value', 7.09) |
||
| 117 | fullSignalK.retrieve().vessels.foo.navigation.speedOverGround.values['n2kFromFile.48'].should.have.property('value', 8) |
||
| 118 | }) |
||
| 119 | |||
| 120 | it('AIS delta produces valid Signal K', function () { |
||
| 121 | this.timeout(4000) // first use of schema validation may have to download schemas so increase timeout from 2 to 4 seconds |
||
| 122 | var aisDelta = { |
||
| 123 | 'updates': [{ |
||
| 124 | 'source': { |
||
| 125 | 'label': 'N2K-1', |
||
| 126 | 'type': 'NMEA2000', |
||
| 127 | 'pgn': 129038, |
||
| 128 | 'src': '43' |
||
| 129 | }, |
||
| 130 | 'timestamp': '2014-08-15T19:00:15.402Z', |
||
| 131 | 'values': [{ |
||
| 132 | 'path': 'navigation.speedOverGround', |
||
| 133 | 'value': 14.81 |
||
| 134 | }, { |
||
| 135 | 'path': 'navigation.courseOverGroundTrue', |
||
| 136 | 'value': 3.4889 |
||
| 137 | }, { |
||
| 138 | 'path': 'navigation.position', |
||
| 139 | 'value': { |
||
| 140 | 'longitude': 24.8142433, |
||
| 141 | 'latitude': 59.865655 |
||
| 142 | } |
||
| 143 | }] |
||
| 144 | }], |
||
| 145 | 'context': 'vessels.urn:mrn:imo:mmsi:276780000' |
||
| 146 | } |
||
| 147 | var fullSignalK = new FullSignalK('urn:mrn:imo:mmsi:276799999', 'mmsi') |
||
| 148 | fullSignalK.addDelta(aisDelta) |
||
| 149 | fullSignalK.retrieve().should.be.validSignalK() |
||
| 150 | }) |
||
| 151 | |||
| 152 | it('Delta with empty path sets content under root', function () { |
||
| 153 | var msg = { |
||
| 154 | 'updates': [{ |
||
| 155 | 'source': { |
||
| 156 | 'label': 'n2kFromFile', |
||
| 157 | 'type': 'NMEA2000', |
||
| 158 | 'pgn': 129794, |
||
| 159 | 'src': '43' |
||
| 160 | }, |
||
| 161 | 'timestamp': '2014-08-15T19:02:31.507Z', |
||
| 162 | 'values': [{ |
||
| 163 | 'path': '', |
||
| 164 | 'value': { |
||
| 165 | 'name': 'WRANGO' |
||
| 166 | } |
||
| 167 | }] |
||
| 168 | }], |
||
| 169 | 'context': 'vessels.urn:mrn:imo:mmsi:276810000' |
||
| 170 | } |
||
| 171 | var fullSignalK = new FullSignalK() |
||
| 172 | fullSignalK.addDelta(msg) |
||
| 173 | var vessel = fullSignalK.retrieve().vessels['urn:mrn:imo:mmsi:276810000'] |
||
| 174 | vessel.should.have.property('name', 'WRANGO') |
||
| 175 | vessel.should.not.have.property('$source') |
||
| 176 | vessel.should.not.have.property('timestamp') |
||
| 177 | vessel.should.not.have.property('pgn') |
||
| 178 | }) |
||
| 179 | |||
| 180 | it('Delta with instance produces proper sources hierarchy', function () { |
||
| 181 | var msg = { |
||
| 182 | 'updates': [{ |
||
| 183 | 'source': { |
||
| 184 | 'label': 'N2K', |
||
| 185 | 'type': 'NMEA2000', |
||
| 186 | 'pgn': 130312, |
||
| 187 | 'src': '36', |
||
| 188 | 'instance': '0' |
||
| 189 | }, |
||
| 190 | 'timestamp': '2015-01-15T16:15:19.628Z', |
||
| 191 | 'values': [{ |
||
| 192 | 'path': 'environment.water.temperature', |
||
| 193 | 'value': 15.2 |
||
| 194 | }] |
||
| 195 | }], |
||
| 196 | 'context': 'vessels.urn:mrn:imo:mmsi:276810000' |
||
| 197 | } |
||
| 198 | var fullSignalK = new FullSignalK() |
||
| 199 | fullSignalK.addDelta(msg) |
||
| 200 | var full = fullSignalK.retrieve() |
||
| 201 | var vessel = full.vessels['urn:mrn:imo:mmsi:276810000'] |
||
| 202 | vessel.environment.water.temperature.should.have.property('value', 15.2) |
||
| 203 | full.sources.should.have.property('N2K') |
||
| 204 | full.sources['N2K'].should.have.property('36') |
||
| 205 | full.sources['N2K']['36'].should.have.property('0') |
||
| 206 | }) |
||
| 207 | |||
| 208 | it('Delta with $source produces sources hierarchy and correct $source reference', function () { |
||
| 209 | var msg = { |
||
| 210 | 'context': 'vessels.urn:mrn:imo:mmsi:276810000', |
||
| 211 | 'updates': [{ |
||
| 212 | '$source': '1W.0316013faeff', |
||
| 213 | 'values': [{ |
||
| 214 | 'path': 'propulsion.engine1.temperature', |
||
| 215 | 'value': 301.837 |
||
| 216 | }] |
||
| 217 | }] |
||
| 218 | } |
||
| 219 | |||
| 220 | var fullSignalK = new FullSignalK('urn:mrn:imo:mmsi:276810000') |
||
| 221 | fullSignalK.addDelta(msg) |
||
| 222 | var full = fullSignalK.retrieve() |
||
| 223 | console.log(JSON.stringify(full)) |
||
|
|
|||
| 224 | full.sources.should.have.property('1W') |
||
| 225 | full.sources['1W'].should.have.property('0316013faeff') |
||
| 226 | var vessel = full.vessels['urn:mrn:imo:mmsi:276810000'] |
||
| 227 | vessel.propulsion.engine1.temperature.should.have.property('$source', '1W.0316013faeff') |
||
| 228 | // full.should.be.validSignalK |
||
| 229 | // TODO make this work! |
||
| 230 | }) |
||
| 231 | |||
| 232 | it('MMSI self is set correctly in full tree', function () { |
||
| 233 | var fullSignalK = new FullSignalK('urn:mrn:imo:mmsi:276810000', null, {}) |
||
| 234 | fullSignalK.retrieve().self.should.equal('vessels.urn:mrn:imo:mmsi:276810000') |
||
| 235 | }) |
||
| 236 | |||
| 237 | it('UUID self is set correctly in full tree', function () { |
||
| 238 | var fullSignalK = new FullSignalK('urn:mrn:signalk:uuid:c0d79334-4e25-4245-8892-54e8ccc8021d', null, {}) |
||
| 239 | fullSignalK.retrieve().self.should.equal('vessels.urn:mrn:signalk:uuid:c0d79334-4e25-4245-8892-54e8ccc8021d') |
||
| 240 | }) |
||
| 241 | }) |
||
| 242 |